/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.phoenix.repository.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;

import javax.jcr.PropertyType;
import javax.jcr.Value;

import net.phoenix.repository.value.BinaryValue;
import net.phoenix.repository.value.BooleanValue;
import net.phoenix.repository.value.DateValue;
import net.phoenix.repository.value.DoubleValue;
import net.phoenix.repository.value.LongValue;
import net.phoenix.repository.value.PathValue;
import net.phoenix.repository.value.ReferenceValue;
import net.phoenix.repository.value.StringValue;

/**
 * Jigsaw server
 * 
 * @author    <a href="shamphone@gmail.com">Li XiongFeng</a>
 * @date      2012-11-7
 * @version   1.0.0
 */
public abstract class ValueTableQueryDao  extends JdbcDao {
	private String table;
	private ResultSet row;
	private PreparedStatement statement;

	/**
	 * create find by ID statement;
	 * @param ID
	 * @param table
	 * @return
	 * @throws SQLException
	 */
	protected abstract PreparedStatement findByIDStatement(String table, String id) throws SQLException;
	
	
	
	/**
	 * 根据ID来查找节点
	 * 
	 * @param ID
	 *            String
	 * @return NodeData
	 * @throws SQLException
	 */
	public int findByID(String table, String id) throws SQLException {
		this.table = table;
		this.statement = this.findByIDStatement(table, id);
		this.row = statement.executeQuery();
		if (this.row.next())
			return 1;
		return 0;
	}
	

	/**
	 * Get ID of current row;
	 * @return
	 * @throws SQLException
	 */
	public String getID() throws SQLException {
		return row.getString("pkid");
	}


	/**
	 * Read a single value property;
	 * 
	 * @param propertyName
	 * @param type
	 * @return
	 * @throws SQLException
	 */
	public Value getSingleValue(String propertyName, int type) throws SQLException {
		String column = propertyName + "[0]";
		return this.readColumn(column, type);
	}
	
	

	/**
	 * 
	 * @param propertyName
	 * @param type
	 * @return
	 * @throws SQLException
	 */
	public Value[] getArrayValue(String propertyName, int type) throws SQLException {
		ArrayList<Value> values = new ArrayList<Value>();
		for (int i = 0; i < ValueTableCreateDao.MULTIPLE_MAX; i++) {
			String column = propertyName + "[" + i + "]";
			Value value = this.readColumn(column, type);
			if (value != null)
				values.add(value);
		}
		return values.toArray(new Value[values.size()]);
	}

	/**
	 * Read value from a column
	 * 
	 * @param column
	 * @param type
	 * @return
	 * @throws SQLException
	 */
	protected Value readColumn(String column, int type) throws SQLException {
		Value value = null;
		switch (type) {
		case PropertyType.BINARY:
			row.getBlob(column);
			if (!row.wasNull()) {
				value = new BinaryValue(new BlobBinary(this.table,column));
			}
			break;
		case PropertyType.BOOLEAN:
			boolean bvalue = row.getBoolean(column);
			if (!row.wasNull())
				value = new BooleanValue(bvalue);
			break;
		case PropertyType.DATE:
			Timestamp tvalue = row.getTimestamp(column);
			if (!row.wasNull())
				value = (new DateValue(tvalue));
			break;
		case PropertyType.DOUBLE:
			double dvalue = row.getDouble(column);
			if (!row.wasNull())
				value = (new DoubleValue(dvalue));
			break;

		case PropertyType.LONG:
			long lvalue = row.getLong(column);
			if (!row.wasNull())
				value = (new LongValue(lvalue));
			break;
		case PropertyType.PATH:
			String pvalue = row.getString(column);
			if (!row.wasNull())
				value = (new PathValue(pvalue));
			break;
		case PropertyType.REFERENCE:
			String rvalue = row.getString(column);
			if (!row.wasNull())
				value = (new ReferenceValue(rvalue));
			break;

		default:
			String svalue = row.getString(column);
			if (!row.wasNull())
				value = (new StringValue(svalue));

		}
		return value;
	}
	
	/**
	 * release all resources;
	 * @throws SQLException
	 */
	public void close() throws SQLException {
		this.row.close();
		this.statement.close();
	}

}
